local skel = fk.CreateSkill {
  name = "lb__cixin",
  tags = {Skill.Compulsory},
  derived_piles = "lb__cixin",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["lb__cixin"] = "慈心",
  --[[
  [":lb__cixin"] = "<b>锁定技</b>，每回合结束时，若你本回合对其他角色使用过牌，你选择一项：<br>"..
  "1.将其中一张牌置于武将牌上，称为「<b>慈心</b>」；<br>"..
  "2.用其中一张牌替换相同花色的「<b>慈心</b>」；<br>"..
  "3.失去1点体力。",
  --]]
  --9.12
  [":lb__cixin"] = "<b>锁定技</b>，每回合结束时，若你本回合对其他角色使用过牌，你选择一项：<br>"..
  "1.将其中一张牌置于武将牌上，称为「<b>慈心</b>」，若已有此花色「<b>慈心</b>」，则替换之；<br>"..
  "2.失去1点体力。",

  ["#lb__cixin"] = "慈心：请选择一张牌加入「<b>慈心</b>」（若有相同花色的「<b>慈心</b>」则替换之），或取消后失去1点体力",

  ["#lb__cixin_1"] = "慈心：请用一张牌替换相同花色的「<b>慈心</b>」",
  ["#lb__cixin_2"] = "慈心：请将一张牌加入「<b>慈心</b>」",

  ["#lb__cixin_change"] = "替换一张「<b>慈心</b>」",
  ["#lb__cixin_put"] = "增加一张「<b>慈心</b>」",
  ["#lb__cixin_lose"] = "失去1点体力",

  ["$lb__cixin1"] = "该休息时，就好好休息。",
  ["$lb__cixin2"] = "药品的话，多少都用得着。",
}
--[[
Fk:addPoxiMethod{
  name = skel.name,
  card_filter = function (to_select, selected, data, extra_data)
    local ids = {}
    for _, flag in ipairs(data) do
      if table.find(flag[2], function (id)
          return table.contains(selected, id)
        end) then
        table.insertTable(ids, flag[2])
      end
    end
    if not table.contains(ids, to_select) then
      if #selected == 1 then
        local card = Fk:getCardById(selected[1])
        local current = Fk:getCardById(to_select)
        return not table.contains(ids, to_select) and card.suit == current.suit
      end
      return #selected < 2
    end
    return false
  end,
  feasible = function (selected, data, extra_data)
    return #selected == 2
  end,
  default_choice = function (data, extra_data)
    for _, cid in ipairs(data[1][2]) do
      for _, id in ipairs(data[2][2]) do
        if Fk:getCardById(id).suit == Fk:getCardById(cid).suit then
          return {id, cid}
        end
      end
    end
    return {}
  end,
  post_select = function (selected, data, extra_data)
    local ids = {}  --排序
    for _, flag in ipairs(data) do
      for _, id in ipairs(flag[2]) do
        if table.contains(selected, id) then
          table.insert(ids, id)
        end
      end
    end
    return ids
  end,
  prompt = "#lb__cixin_1",
}
--]]
skel:addEffect(fk.TurnEnd, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      return use.from == player and table.find(use.tos, function (to)
        return to ~= player
      end) ~= nil
    end, Player.HistoryTurn) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local ids = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use.from == player and table.find(use.tos, function (to)
        return to ~= player
      end) then
        local id = use.card:getEffectiveId()
        if id and table.contains(room.discard_pile, id) then
          table.insertIfNeed(ids, id)
        end
      end
    end, Player.HistoryTurn)
    --[[
    local choices = {"#lb__cixin_lose"}
    local all = {"#lb__cixin_change", "#lb__cixin_put", "#lb__cixin_lose"}
    if #ids > 0 then
      table.insert(choices, 1, "#lb__cixin_put")
      if #player:getPile(skel.name) > 0 and table.find(player:getPile(skel.name), function (id)
          return table.find(ids, function (cid)
            return Fk:getCardById(id).suit == Fk:getCardById(cid).suit
          end)
        end) then
        table.insert(choices, 1, "#lb__cixin_change")
      end
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      all_choices = all,
      skill_name = skel.name,
    })
    event:setCostData(self, {choice = choice, cards = ids})
    return true
    --]]
    if #ids > 0 then
      local cards = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        pattern = tostring(Exppattern {id = ids}),
        expand_pile = ids,
        prompt = "#lb__cixin",
        cancelable = true,
      })
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if cards and #cards > 0 then
      if #player:getPile(skel.name) > 0 then
          local id = table.find(player:getPile(skel.name), function (id)
            return Fk:getCardById(id).suit == Fk:getCardById(cards[1]).suit
          end)
          if id then
            room:moveCardTo(id, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, skel.name, true, player)
          end
      end
      player:addToPile(skel.name, cards, true, skel.name, player)
    else
      room:loseHp(player, 1, skel.name)
    end
    --[[
    local choice = event:getCostData(self).choice
    if choice == "#lb__cixin_lose" then
      room:loseHp(player, 1, skel.name)
    elseif choice == "#lb__cixin_change" then
      local ids = room:askToPoxi(player, {
        poxi_type = skel.name,
        cancelable = false,
        data = {
          { skel.name, player:getPile(skel.name) },
          { "pile_discard", cards },
        },
      })
      if #ids == 2 then
        player:addToPile(skel.name, ids[2], true, skel.name, player)
        room:moveCardTo(ids[1], Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, skel.name, true, player)
      end
    elseif choice == "#lb__cixin_put" then
      if #cards > 1 then
        cards = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          pattern = tostring(Exppattern {id = cards}),
          expand_pile = cards,
          prompt = "#lb__cixin_2",
          cancelable = false,
        })
      end
      player:addToPile(skel.name, cards, true, skel.name, player)
    end
    --]]
  end,
})


skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel